package model

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ PmsAttrModel = (*customPmsAttrModel)(nil)

type (
	// PmsAttrModel is an interface to be customized, add more methods here,
	// and implement the added methods in customPmsAttrModel.
	PmsAttrModel interface {
		pmsAttrModel
		FindByPageAndKey(ctx context.Context, key string, categoryId, limit, page int64) ([]PmsAttr, error)
	}

	customPmsAttrModel struct {
		*defaultPmsAttrModel
	}
)

func (c customPmsAttrModel) FindByPageAndKey(ctx context.Context, key string, categoryId, limit, page int64) ([]PmsAttr, error) {
	var list []PmsAttr
	searchKey := "%" + key + "%"
	query := fmt.Sprintf("SELECT * FROM pms_attr  WHERE catelog_id = ? and attr_name LIKE ? LIMIT ? OFFSET ?")
	err := c.conn.QueryRowsCtx(ctx, &list, query, categoryId, searchKey, limit, page-1)
	if err != nil {
		return nil, err
	}
	return list, nil
}

// NewPmsAttrModel returns a model for the database table.
func NewPmsAttrModel(conn sqlx.SqlConn) PmsAttrModel {
	return &customPmsAttrModel{
		defaultPmsAttrModel: newPmsAttrModel(conn),
	}
}
